<form action="index.php?option=com_question" method="post" class="adminform" name="adminForm" id="adminForm">

	<fieldset id="filter-bar">

		<div class="filter-search fltlft">

			<label class="filter-search-lbl" for="filter_search">フィルタ</label>

			<input style="width: 300px;" type="text" name="keyword" id="keyword" value="<?php echo JRequest::getVar('keyword') ?>" />



			<button id="search" type="button" onclick="Joomla.submitbutton('search')">検索</button>

			<button type="button" id="clear" onclick="document.id('keyword').value='';this.form.submit();">クリア</button>
            
		</div>
        <div style="float: right;">
            <button type="submit" class="btn" href="index.php?option=com_questionare">アンケート管理</button>
        </div>
        <div style="float: right;">
                <button type="submit" class="btn" href="index.php?option=com_questionpage">ページ管理</button>
        </div>

		

	</fieldset>

	<div class="clr"> </div>

	<table class="adminlist">

		<thead>

			<tr>

				<th width="5%">

					<input type="checkbox" name="checkall-toggle" value="" title="Check All" onclick="Joomla.checkAll(this)"/>

				</th>

				<th width="10%">

					<label class="labelth">質問ID</label>				

                </th>

				<th width="25%">

					<label class="labelth">質問</label>	

                </th>

				<th width="10%">

					<label class="labelth">タイプ</label>	

                </th>

				<th width="20%">

					<label class="labelth">アンケート名</label>

                </th>

                <th width="20%">

					<label class="labelth">ページ名</label>	

                </th>

                 <th width="10%">

					<label class="labelth">必須</label>	

                </th>

                <!-- 

				<th width="15%">

					<a href="#">公開/非公開</a>

                </th>

				 -->

			</tr>

		</thead>

		<tfoot>

			<tr>

				<td colspan="15">

					<?php echo $this->pagination->getListFooter(); ?>			

                </td>

			</tr>

		</tfoot>

		<tbody id="result">

        <?php

            $result = $this->result;

            if(!empty($result)){

                foreach ($result as $key=>$ques){

                $row_cls = $key % 2;

                ($ques->QUES_PUBLISHED) == 1 ? $public = '公開' : $public = '非公開'; //Check flag public or no public

                ($ques->QUES_REQUIRED) == 1 ? $required = '必須' : $required = '';

                //Kiem tra type cau hoi

                if($ques->QUES_TYPE == 1){

                    $type = '複数回答可';

                }

                elseif($ques->QUES_TYPE == 2){

                    $type = '単一回答';

                }

                else{

                    $type = '自由回答';

                }

        ?>

            <tr class="row<?php echo $row_cls; ?>">

    			<td class="center">
                <input type="checkbox" class="chbbox" id="cb<?php echo $ques->QUES_CODE ?>" name="cid[]" value="<?php echo $ques->QUES_CODE; ?>" onclick="Joomla.isChecked(this.checked);" />
                
                <?php 
                    $model = $this->getModel();
                    $serq = $model->getSERQ($ques->QUES_SERQ_CODE); //Lay SERQ theo QUES_SERQ_CODE
                 ?>
                <input type="hidden" name="puplish" id="publish<?php echo $ques->QUES_CODE ?>" value="<?php echo $serq->SERQ_PUBLISHED; ?>" />
                <?php if(strtotime("now") <= strtotime($serq->SERQ_START)){ $temp = 0;} else { $temp = 1;} ?>
                <input type="hidden" name="date" id="date<?php echo $ques->QUES_CODE ?>" value="<?php echo $temp; ?>" />
            </td>
    			<td class="center">
                <?php 
                    $model = $this->getModel();
                    $serq = $model->getSERQ($ques->QUES_SERQ_CODE); //Lay SERQ theo QUES_SERQ_CODE
                    if($serq->SERQ_PUBLISHED =='0' || ($serq->SERQ_PUBLISHED =='1' && ( strtotime("now") <= strtotime($serq->SERQ_START)))){ ?>
                    <a href="index.php?option=com_question&task=edit&cid=<?php echo $ques->QUES_CODE; ?>"><?php echo $ques->QUES_CODE; ?></a>
                <?php }else{ ?>
                        <?php echo $ques->QUES_CODE;?>
                <?php } ?> 
                </td>
    			<td> 
                <?php 
                    $model = $this->getModel();
                    $serq = $model->getSERQ($ques->QUES_SERQ_CODE); //Lay SERQ theo QUES_SERQ_CODE
                    if($serq->SERQ_PUBLISHED =='0' || ($serq->SERQ_PUBLISHED =='1' && ( strtotime("now") <= strtotime($serq->SERQ_START)))){ ?>
                    <a href="index.php?option=com_question&task=edit&cid=<?php echo $ques->QUES_CODE; ?>"><?php echo $ques->QUES_TITLE; ?></a>
                <?php }else{ ?>
                        <?php echo $ques->QUES_TITLE;?>
                <?php } ?> 
                </td>
    			<td class="center"><?php echo $type; ?></td>
                <td class='center'>
                <?php 
                    $model = $this->getModel();

                    $this->serq = $model->getSERQ($ques->QUES_SERQ_CODE); //Lay SERQ theo QUES_SERQ_CODE
                    echo $this->serq->SERQ_TITLE;
                ?>
                </td>

                <?php 

                    $model = $this->getModel();

                    $this->pagq = $model->getPAGQ($ques->QUES_PAGQ_CODE); //Lay PAGQ theo QUES_PAGQ_CODE

                    if($this->serq != null){

                        echo "<td class='center'>$this->pagq</td>";

                    }else{

                        echo "<td class='center'></td>";

                    }

                ?>

                <td class="center"><?php echo $required; ?></td>

                <!-- <td class="center"><?php echo $public; ?></td> -->

            </tr>

        <?php

                }

            }

            else{

                echo "<tr class='row0'>

                            <td colspan='8' class='center'>データは見つかりませんでした</td>

                    </tr>";

            }

        ?>

		</tbody>

	</table>

	<div>

		<input type="hidden" name="task" value="">

		<input type="hidden" name="boxchecked" value="0">

</form>

<script>

    jq('#clear').click(function(event){

        jq('#keyword').val("");

    });

    Joomla.submitbutton = function(task){
    var check = "0";
    if(task == 'remove'){
        jq(".chbbox").each(function(){
            if(jq(this).attr('checked')){
                var id = jq(this).val();
                var publish = jq("#publish"+id).val();
                var date = jq("#date"+id).val();
                if(publish == "1" && date == "1"){
                    alert("本質問のアンケートは公開されましたから、編集できません。")
                    check = "1";
                    return true;
                }
                else{
                    if(!confirm('選択された質問を削除します。よろしいですか？')){
                        
                        return false;
                    }
                }
            }
            });
        
    }
    
       if(task == 'edit'){
            jq(".chbbox").each(function(){
            if(jq(this).attr('checked')){
                var id = jq(this).val();
                var publish = jq("#publish"+id).val();
                var date = jq("#date"+id).val();
                if(publish == "1" && date == "1"){
                    alert("本質問のアンケートは公開されましたから、編集できません。")
                    check = "1";
                    return false;
                }
            }
            });
       }
       if(check == "0"){
            Joomla.submitform(task, document.getElementById('adminForm'));
       }else{
            Joomla.submitform("search", document.getElementById('adminForm'));
       }

 }

</script>